package com.qzsoft.zhjd.machine.modules.machinedaily.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.machine.modules.machinedaily.dto.MachineDailyListDto;
import com.qzsoft.zhjd.machine.modules.machinedaily.entity.MachineDailyEntity;
import com.qzsoft.zhjd.machine.modules.machinedaily.entity.MachineDailyWorkrecordsEntity;
import com.qzsoft.zhjd.machine.modules.machinedaily.mapper.MachineDailyMapper;
import com.qzsoft.zhjd.machine.modules.machinedaily.service.MachineDailyService;
import com.qzsoft.zhjd.machine.modules.machinedaily.service.MachineDailyWorkrecordsService;

/**
 * @ClassName MachineDailyServiceImpl
 * @Description 机械日报业务接口实现
 * @author linhuawei
 * @since 2021-11-05
 */
@Service
public class MachineDailyServiceImpl extends ServiceImpl<MachineDailyMapper, MachineDailyEntity>
        implements MachineDailyService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private MachineDailyWorkrecordsService machineDailyWorkrecordsService;

    @Override
    public IPage<MachineDailyListDto> pageDto(Map<String, Object> params) {
        IPage<MachineDailyListDto> page = baseServiceComponent.getPage(params, MachineDailyListDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    public IPage<MachineDailyEntity> page(Map<String, Object> params) {
        // 分页数据
        if(null == params) {
            params = new HashMap<String, Object>();
        }
        IPage<MachineDailyEntity> page = baseServiceComponent.getPage(params, MachineDailyEntity.class);
        
        // 查询条件
        QueryWrapper<MachineDailyEntity> queryWrapper = createQueryWrapper(params);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public IPage<MachineDailyEntity> dailyWithWorkrecords(Map<String, Object> params) {
        // 处理查询条件
        if(null == params) {
            params = new HashMap<String, Object>();
        }
        
        // 分页数据
        IPage<MachineDailyEntity> page = baseServiceComponent.getPage(params, MachineDailyEntity.class);
        QueryWrapper<MachineDailyEntity> queryWrapper = createQueryWrapper(params);
        page = baseMapper.selectPage(page, queryWrapper);
        
        // 获取工时详情
        page.getRecords().forEach(item->{
            QueryWrapper<MachineDailyWorkrecordsEntity> workrecordsQueryWrapper = new QueryWrapper<>();
            workrecordsQueryWrapper.eq("daily_id", item.getId());
            workrecordsQueryWrapper.orderByDesc("start_time");
            List<MachineDailyWorkrecordsEntity> list = machineDailyWorkrecordsService.list(workrecordsQueryWrapper);
            item.setWorkrecords(list);
        });
        
        return page;
    }

    @Override
    public MachineDailyEntity monthCountByMachineId(String machineId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String month = sdf.format(new Date());
        return baseMapper.monthCount(machineId, month);
    }

    @Override
    public List<MachineDailyEntity> listForMachine(String machineId, Map<String, Object> params) {
        // 设置查询条件
        QueryWrapper<MachineDailyEntity> queryWrapper = new QueryWrapper<MachineDailyEntity>();
        queryWrapper.eq("machine_id", machineId);
        if(ObjectUtils.isNotEmpty(params.get("startTime"))) {
            queryWrapper.ge("report_date", params.get("startTime"));
        }
        if(ObjectUtils.isNotEmpty(params.get("endTime"))) {
            queryWrapper.le("report_date", params.get("endTime"));
        }
        queryWrapper.orderByDesc("report_date");
        return baseMapper.selectList(queryWrapper);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MachineDailyEntity entity, Date reportDate) {
        // 格式化日期数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String reportDateStr = sdf.format(reportDate);

        // 删除已经存在的数据
        String machineId = entity.getMachineId();
        QueryWrapper<MachineDailyEntity> dailyWrapper = new QueryWrapper<MachineDailyEntity>();
        dailyWrapper.eq("machine_id", machineId);
        dailyWrapper.eq("report_date", reportDateStr);
        baseMapper.delete(dailyWrapper);

        // 删除已经存在的工时信息
        QueryWrapper<MachineDailyWorkrecordsEntity> workrecordsWrapper = new QueryWrapper<MachineDailyWorkrecordsEntity>();
        workrecordsWrapper.eq("machine_id", machineId);
        workrecordsWrapper.eq("report_date", reportDateStr);
        machineDailyWorkrecordsService.remove(workrecordsWrapper);

        // 执行添加操作
        entity.setReportDate(reportDate);
        super.save(entity);

        // 添加工时记录
        List<MachineDailyWorkrecordsEntity> workrecords = entity.getWorkrecords();
        if (ObjectUtils.isNotEmpty(workrecords)) {
            for (MachineDailyWorkrecordsEntity item : workrecords) {
                item.setReportDate(reportDate);
                item.setDailyId(entity.getId());
            }
            machineDailyWorkrecordsService.saveBatch(workrecords);
        }
    }
    
    
    private QueryWrapper<MachineDailyEntity> createQueryWrapper(Map<String, Object> params){
     // 获取查询条件
        QueryWrapper<MachineDailyEntity> queryWrapper = new QueryWrapper<MachineDailyEntity>();
        if(ObjectUtils.isNotEmpty(params.get("machineId"))) {
            queryWrapper.eq("machine_id", params.get("machineId"));
        }
        if(ObjectUtils.isNotEmpty(params.get("startTime"))) {
            queryWrapper.ge("report_date", params.get("startTime"));
        }
        if(ObjectUtils.isNotEmpty(params.get("endTime"))) {
            queryWrapper.le("report_date", params.get("endTime"));
        }
        queryWrapper.orderByDesc("report_date");
        return queryWrapper;
    }

}
